package com.yita.fruit.servlets;

import com.yita.fruit.dao.FruitDAO;
import com.yita.fruit.dao.Impl.FruitDaoImpl;
import com.yita.fruit.pojo.Fruit;
import com.yita.myssm.myspringmvc.ViewBaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/index")
public class IndexServlet extends ViewBaseServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int page = 1;
        String pageNoStr = req.getParameter("pageNo");
        if (pageNoStr != null&&pageNoStr.length()>0){
            page=Integer.parseInt(pageNoStr);
        }
        FruitDAO fruitDao = new FruitDaoImpl();
        List<Fruit> fruitList = fruitDao.getFruitList(page,5);

        HttpSession session = req.getSession();

        Integer pageCount = (Integer) session.getAttribute("pageCount");
        if (pageCount == null) {
            int fruitCount = fruitDao.getFruitCount();
            pageCount = fruitCount / 5+1;
            session.setAttribute("pageCount", pageCount);
        }


        session.setAttribute("fruitList", fruitList);
        session.setAttribute("pageNo", page);
        //逻辑视图名称:index
        //物理视图名称: view-prefix +逻辑视图名称 + view-suffix
        //所以index的物理试题名称是:/ index .html

        processTemplate("index", req, resp);

    }
}
